<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="企业名称" prop="enterpriseName">
        <el-input
          v-model="queryParams.enterpriseName"
          placeholder="请输入企业名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退租日期" prop="endTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.endTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择退租日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="租赁模式" prop="leaseMode">
        <el-input
          v-model="queryParams.leaseMode"
          placeholder="请输入租赁模式"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="经办人" prop="approval">
        <el-input
          v-model="queryParams.approval"
          placeholder="请输入经办人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['life:feApartmentCancel:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['life:feApartmentCancel:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['life:feApartmentCancel:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['life:feApartmentCancel:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="feApartmentCancelList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="企业名称" align="center" prop="enterpriseName" />
      <el-table-column label="租赁模式" align="center" prop="leaseMode" />
      <el-table-column label="合同总面积" align="center" prop="contractArea" />
      <el-table-column label="退租日期" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="账单总计" align="center" prop="total" />
      <el-table-column label="租金总计" align="center" prop="rentalTotal" />
      <el-table-column label="物业费总计" align="center" prop="propertyTotal" />
      <el-table-column label="水电气费用" align="center" prop="electricFee" />
      <el-table-column label="设施赔偿费" align="center" prop="facilityNoun" />
      <el-table-column label="保证金总计" align="center" prop="marginTotal" />
      <el-table-column label="备注" align="center" prop="remark" />
<!--      <el-table-column label="经办人" align="center" prop="approval" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
<!--          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['life:feApartmentCancel:edit']"
          >修改</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDetail(scope.row)"
            v-hasPermi="['life:feApartmentCancel:edit']"
          >详情</el-button>
<!--          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['life:feApartmentCancel:remove']"
          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改公寓退租记录对话框 -->
    <el-dialog :title="title"  :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :disabled="isdisabled" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="企业id" prop="enterpriseId" >
          <el-input v-model="form.enterpriseId" placeholder="请输入企业id" />
        </el-form-item>
        <el-form-item label="租赁模式" prop="leaseMode" >
          <el-input v-model="form.leaseMode" placeholder="请输入租赁模式" />
        </el-form-item>
        <el-form-item label="合同总面积" prop="contractArea">
          <el-input v-model="form.contractArea" placeholder="请输入合同总面积" />
        </el-form-item>
        <el-form-item label="退租日期" prop="endTime">
          <el-date-picker clearable size="small"
                          v-model="form.endTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择退租日期">
          </el-date-picker>
        </el-form-item>

        <el-divider content-position="center">公寓退租详情信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddFeApartmentCancelDetail">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteFeApartmentCancelDetail">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="feApartmentCancelDetailList" :row-class-name="rowFeApartmentCancelDetailIndex" @selection-change="handleFeApartmentCancelDetailSelectionChange" ref="feApartmentCancelDetail">
          <el-table-column type="selection" width="50" align="center" />
          <!--<el-table-column label="序号" align="center" prop="index" width="50"/>-->
<!--          <el-table-column label="退租id" prop="cancelId">
            <template slot-scope="scope">
              <el-input v-model="scope.row.cancelId" placeholder="请输入退租id" />
            </template>
          </el-table-column>-->
<!--          <el-table-column label="企业id" prop="enterpriseId">
            <template slot-scope="scope">
              <el-input v-model="scope.row.enterpriseId" placeholder="请输入企业id" />
            </template>
          </el-table-column>-->
<!--          <el-table-column label="房间id" prop="roomId">
            <template slot-scope="scope">
              <el-input v-model="scope.row.roomId" placeholder="请输入房间id" />
            </template>
          </el-table-column>-->
          <el-table-column label="房间编号" prop="roomCode">
            <template slot-scope="scope">
              <el-input v-model="scope.row.roomCode" placeholder="请输入房间编号" />
            </template>
          </el-table-column>
          <el-table-column label="租金" prop="rental">
            <template slot-scope="scope">
              <el-input v-model="scope.row.rental" placeholder="请输入租金" />
            </template>
          </el-table-column>
          <el-table-column label="物业费" prop="property">
            <template slot-scope="scope">
              <el-input v-model="scope.row.property" placeholder="请输入物业费" />
            </template>
          </el-table-column>
          <el-table-column label="水电气费用" prop="electricFee">
            <template slot-scope="scope">
              <el-input v-model="scope.row.electricFee" placeholder="请输入水电气费用" />
            </template>
          </el-table-column>
          <el-table-column label="设施赔偿费" prop="facilityNoun">
            <template slot-scope="scope">
              <el-input v-model="scope.row.facilityNoun" placeholder="请输入设施赔偿费" />
            </template>
          </el-table-column>
          <el-table-column label="保证金" prop="margin">
            <template slot-scope="scope">
              <el-input v-model="scope.row.margin" placeholder="请输入保证金" />
            </template>
          </el-table-column>
<!--          <el-table-column label="退租时间" prop="cancelDate">
            <template slot-scope="scope">
              <el-input v-model="scope.row.cancelDate" placeholder="请输入退租时间" />
            </template>-->
          </el-table-column>
          <el-table-column label="备注" prop="remark">
            <template slot-scope="scope">
              <el-input v-model="scope.row.remark" placeholder="请输入备注" />
            </template>
          </el-table-column>
<!--          <el-table-column label="经办人" prop="approval">
            <template slot-scope="scope">
              <el-input v-model="scope.row.approval" placeholder="请输入经办人" />
            </template>
          </el-table-column>-->
        </el-table>
        <el-form-item label="账单总计" prop="total">
          <el-input v-model="form.total" placeholder="请输入账单总计" />
        </el-form-item>
        <el-form-item label="租金总计" prop="rentalTotal">
          <el-input v-model="form.rentalTotal" placeholder="请输入租金总计" />
        </el-form-item>
        <el-form-item label="物业费总计" prop="propertyTotal">
          <el-input v-model="form.propertyTotal" placeholder="请输入物业费总计" />
        </el-form-item>
        <el-form-item label="水电气费用" prop="electricFee">
          <el-input v-model="form.electricFee" placeholder="请输入水电气费用" />
        </el-form-item>
        <el-form-item label="设施赔偿费" prop="facilityNoun">
          <el-input v-model="form.facilityNoun" placeholder="请输入设施赔偿费" />
        </el-form-item>
        <el-form-item label="保证金总计" prop="marginTotal">
          <el-input v-model="form.marginTotal" placeholder="请输入保证金总计" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="经办人" prop="approval">
          <el-input v-model="form.approval" placeholder="请输入经办人" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFeApartmentCancel, getFeApartmentCancel, delFeApartmentCancel, addFeApartmentCancel, updateFeApartmentCancel } from "@/api/life/feApartmentCancel";

export default {
  name: "FeApartmentCancel",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedFeApartmentCancelDetail: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      isdisabled: false,
      // 公寓退租记录表格数据
      feApartmentCancelList: [],
      // 公寓退租详情表格数据
      feApartmentCancelDetailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        enterpriseId: null,
        endTime: null,
        leaseMode: null,
        contractArea: null,
        total: null,
        rentalTotal: null,
        propertyTotal: null,
        electricFee: null,
        facilityNoun: null,
        marginTotal: null,
        approval: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询公寓退租记录列表 */
    getList() {
      this.loading = true;
      listFeApartmentCancel(this.queryParams).then(response => {
        this.feApartmentCancelList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        enterpriseId: null,
        endTime: null,
        leaseMode: null,
        contractArea: null,
        total: null,
        rentalTotal: null,
        propertyTotal: null,
        electricFee: null,
        facilityNoun: null,
        marginTotal: null,
        remark: null,
        approval: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.feApartmentCancelDetailList = [];
      this.isdisabled = null;
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加公寓退租记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getFeApartmentCancel(id).then(response => {
        this.form = response.data;
        this.feApartmentCancelDetailList = response.data.feApartmentCancelDetailList;
        this.open = true;
        this.title = "修改公寓退租记录";
      });
    },
    /** 查看详情操作 */
    handleDetail(row) {
      this.reset();
      this.isdisabled = true;
      const id = row.id || this.ids
      getFeApartmentCancel(id).then(response => {
        this.form = response.data;
        this.feApartmentCancelDetailList = response.data.feApartmentCancelDetailList;
        this.open = true;
        this.title = "查看公寓退租详情";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.feApartmentCancelDetailList = this.feApartmentCancelDetailList;
          if (this.form.id != null) {
            updateFeApartmentCancel(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFeApartmentCancel(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除公寓退租记录编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delFeApartmentCancel(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
	/** 公寓退租详情序号 */
    rowFeApartmentCancelDetailIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 公寓退租详情添加按钮操作 */
    handleAddFeApartmentCancelDetail() {
      let obj = {};
      //obj.cancelId = "";
      //obj.enterpriseId = "";
      obj.roomId = "";
      obj.rental = "";
      obj.property = "";
      obj.electricFee = "";
      obj.facilityNoun = "";
      obj.margin = "";
      obj.cancelDate = "";
      obj.remark = "";
      obj.approval = "";
      this.feApartmentCancelDetailList.push(obj);
    },
    /** 公寓退租详情删除按钮操作 */
    handleDeleteFeApartmentCancelDetail() {
      if (this.checkedFeApartmentCancelDetail.length == 0) {
        this.$alert("请先选择要删除的公寓退租详情数据", "提示", { confirmButtonText: "确定", });
      } else {
        this.feApartmentCancelDetailList.splice(this.checkedFeApartmentCancelDetail[0].index - 1, 1);
      }
    },
    /** 单选框选中数据 */
    handleFeApartmentCancelDetailSelectionChange(selection) {
      if (selection.length > 1) {
        this.$refs.feApartmentCancelDetail.clearSelection();
        this.$refs.feApartmentCancelDetail.toggleRowSelection(selection.pop());
      } else {
        this.checkedFeApartmentCancelDetail = selection;
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('life/feApartmentCancel/export', {
        ...this.queryParams
      }, `life_feApartmentCancel.xlsx`)
    }
  }
};
</script>
